/**
 * APP启动页(可以摆放广告或一些app的茶话介绍)
 * @Author : andy.wangzhh
 * @Timestamp : 2016-09-05
 */
var deviceone = require("deviceone");
var app = sm("do_App");
var page = sm("do_Page");
var global = sm("do_Global");
var nf = sm("do_Notification");
var urlUser = require("url/user");
var helperAd = require("helper/ad");
var openHelper = require("helper/open");
var upgradeHelper = require("helper/upgrade");
var pubUrl = require("url/pub");
var httpSilence = require("helper/httpSilence");
var external = sm("do_External");
var initdata = sm("do_InitData");
var imHelper = require("helper/im");
var qqCacher = require("helper/qqCacher");
var gl = require("helper/global");
var userUtil = require("helper/user");
qqCacher.currentContentVersion();
qqCacher.upgradePageIsOpen("2");
qqCacher.isFromStartup("1");
var pushHelper = require("helper/push");
pushHelper.handleAudience();

var dingweiUtil = require("helper/bdmap");
dingweiUtil.initDingwei();
var deviceUtil = require("helper/device");
deviceUtil.initDevice();
var osType = deviceUtil.getOsType();

var indexPage = "source://view/main.ui";
page.on("back", function(){
	deviceUtil.home();
});

page.on("result", function(data) {
	qqCacher.firstTimeOpen("2");
	app.openPage(indexPage);
	imHelper.addMsgListener();//从新回到这个页面时一定要重新监听IM,否则收不到消息
});

global.on("background", function(data, e) {
	httpSilence.post(urlUser.url.value.changeBg, {"isBackground":"1","osType":osType}, function(data) {});
});
global.on("foreground", function(data, e) {
	httpSilence.post(urlUser.url.value.changeBg, {"isBackground":"2","osType":osType}, function(data) {});
});
page.on("loaded", function(){
	initdata.copy(["initdata://share_logo.jpg"], "data://", function(data, e) {
	});
	var needTime = 3000;
	httpSilence.post(urlUser.url.base.getUserBaseConfig, {type:2}, function(data) {
		var interestData = '[';
	    for(var i=0;i<data.data.length;i++)
	    	{
	    	var obj='{"val":"'+data.data[i].id+'","name":"'+data.data[i].name+'"}';
	    	interestData += obj;
	    	if(i!=data.data.length-1)
	    	    interestData += ',';
	    	}
		interestData += ']';
	    //write to local storage
 
	    userUtil.saveUserPara(1,'interest',interestData);
	    
	});
	httpSilence.post(urlUser.url.base.getUserBaseConfig, {type:1}, function(data) {
		var industryData = '[';
		    for(var i=0;i<data.data.length;i++)
		    	{
		    	var obj='{"val":"'+data.data[i].id+'","name":"'+data.data[i].name+'"}';
		    	industryData += obj;
		    	if(i!=data.data.length-1)
		    		industryData += ',';
		    	}
		industryData += ']';
		//write to local storage
	    userUtil.saveUserPara(1,'industry',industryData);
	});
	httpSilence.post(urlUser.url.base.getUserBaseConfig, {type:3}, function(data) {
		var delicacyData = '[';
		    for(var i=0;i<data.data.length;i++)
		    	{
		    	var obj='{"val":"'+data.data[i].id+'","name":"'+data.data[i].name+'"}';
		    	delicacyData += obj;
		    	if(i!=data.data.length-1)
		    		delicacyData += ',';
		    	}
		delicacyData += ']';
		//write to local storage
	    userUtil.saveUserPara(1,'delicacy',delicacyData);
	});
	httpSilence.post(urlUser.url.base.getUserBaseConfig, {type:4}, function(data) {
		var hometownData = '[';
		    for(var i=0;i<data.data.length;i++)
		    	{
		    	var obj='{"val":"'+data.data[i].id+'","name":"'+data.data[i].name+'"}';
		    	hometownData += obj;
		    	if(i!=data.data.length-1)
		    		hometownData += ',';
		    	}
		hometownData += ']';
		//write to local storage
	    userUtil.saveUserPara(1,'hometown',hometownData);
	});
	httpSilence.post(urlUser.url.base.getUserBaseConfig, {type:5}, function(data) {
		var entertainmentData = '[';
		    for(var i=0;i<data.data.length;i++)
		    	{
		    	var obj='{"val":"'+data.data[i].id+'","name":"'+data.data[i].name+'"}';
		    	entertainmentData += obj;
		    	if(i!=data.data.length-1)
		    		entertainmentData += ',';
		    	}
		entertainmentData += ']'; 
		//write to local storage
	    userUtil.saveUserPara(1,'entertainment',entertainmentData);
	});
	httpSilence.post(pubUrl.upgrade.version.getByVersion,{"currVersion":qqCacher.currentContentVersion(),"platform":deviceUtil.getOsType()},function(data){
		var updFile = data.updFile;
		var toVersion = data.toVersion;
		upgradeHelper.doUpdateByVersion(updFile,toVersion,function(){
		});
		nf.toast("数据加载中...");
		/*
		if(upgrade&&upgrade!=null&&upgrade.isShowUpdContent&&upgrade.isShowUpdContent==1&&upgrade.contentList){
			var rootStartup = ui("$");
			var addUpload = rootStartup.add("uploadBox","source://view/public/upgrade/updContent.ui", 0, 0);
			page.fire("uploadBoxList", upgrade.contentList);
			page.fire("uploadBox", "true");
		}
		*/
	});
	httpSilence.post(urlUser.url.base.getLogo,{},function(data){
		if(data&&data.userLogo&&data.userLogo!==""){
			var userHelper = require("helper/user");
			userHelper.userLogo(data.userLogo);
			userHelper.hasIdentityAuth(data.hasIdentityAuth);
			userHelper.hasCommunityAuth(data.hasCommunityAuth);
		}
	});
	var firstTime = qqCacher.firstTimeOpen();
	if(firstTime&&firstTime!=null&&firstTime!=="undefined"&&firstTime==="2"){
		qqCacher.firstTimeOpen("2");
		ui("startLayout").visible = true;
		helperAd.getAd(helperAd.type.STARTUP, function(data) {
			ui("startAdImg").source = data.list[0].pathImg;
			ui("do_ALayout_6").on("touch",function(e){
				if(data.list[0].href&&data.list[0].href!==""){
					var href=data.list[0].href;
					if(href.indexOf("http://")<0){
						href="http://"+href;
					}
					external.openURL(href);
				}
			});
		});
		gl.setTimeout(function(){
			imHelper.initIM();
			imHelper.loginIM(function(){
				app.openPage({source:indexPage,id:"qquan_page_index"});
			});
		},needTime,"timerStart");
	}else{
		ui("startALout").visible = true;
		qqCacher.firstTimeOpen("2");
		var startupList = ui("startupList");
		var listdata = mm("do_ListData");
		startupList.bindItems(listdata);
		var startupData = [
       		{"defaultImage":"source://image/startup/s01.png"},
       		{"defaultImage":"source://image/startup/s02.png"},
       		{"defaultImage":"source://image/startup/s03.png","path":indexPage}];
       		listdata.addData(startupData);
       		startupList.refreshItems();
		helperAd.getAd(helperAd.type.NEWUSER_STARTUP, function(data) {
			var alldata = listdata.getRange(0);
			for(var i = 0;i<alldata.length;i++){
				if(null!=data.list&&null!=data.list[i]){
					deviceone.print(data.list[i].pathImg);
					alldata[i].pathImg=data.list[i].pathImg;
					alldata[i].path=data.list[i].path;
					listdata.updateOne(i, alldata[i]);
				}
				
			}
			startupList.refreshItems();
       		
		});
		var startBtn0 = ui("startBtn0"),startBtn1 = ui("startBtn1"),startBtn2 = ui("startBtn2");
		var alboxcount =[startBtn0,startBtn1,startBtn2];
		startupList.on("indexChanged", function(data, e) {
			var index = data;
			for(var i = 0; i<3; i++){
				if(data == i){
					alboxcount[i].width = 40;
				}else{
					alboxcount[i].width = 10;
				}
				alboxcount[i].redraw();
				
			}
		});
	}
});